package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.yxinsur.imc.business.domain.ImcBrokerRewardsRecord;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.BrokerTrainingRecordForm;
import com.yxinsur.imc.business.vo.BrokerTrainingRecordVo;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxinsur.imc.business.mapper.ImcBrokerTrainingRecordMapper;
import com.yxinsur.imc.business.domain.ImcBrokerTrainingRecord;
import com.yxinsur.imc.business.service.IImcBrokerTrainingRecordService;

/**
 * 代理人培训记录Service业务层处理
 *
 * @author imc
 * @date 2021-07-26
 */
@Service
public class ImcBrokerTrainingRecordServiceImpl extends ServiceImpl<ImcBrokerTrainingRecordMapper, ImcBrokerTrainingRecord> implements IImcBrokerTrainingRecordService
{
    private static final Logger logger = LoggerFactory.getLogger(ImcBrokerTrainingRecordServiceImpl.class);

    @Autowired
    private ImcBrokerTrainingRecordMapper imcBrokerTrainingRecordMapper;

    /**
     * 查询代理人培训记录列表
     *
     * @param brokerTrainingRecordForm 代理人培训记录
     * @return 代理人培训记录
     */
    @Override
    public List<BrokerTrainingRecordVo> selectImcBrokerTrainingRecordList(BrokerTrainingRecordForm brokerTrainingRecordForm)
    {
        logger.info("查询代理人培训记录列表查询，条件：{}",brokerTrainingRecordForm.toString());
        return imcBrokerTrainingRecordMapper.selectImcBrokerTrainingRecordList(brokerTrainingRecordForm, SecurityUtils.getTenantId());
    }

    /**
     * 新增代理人培训记录
     *
     * @param brokerTrainingRecordForm 代理人培训记录
     * @return 结果
     */
    @Override
    public int insertImcBrokerTrainingRecord(BrokerTrainingRecordForm brokerTrainingRecordForm)
    {
        logger.info("新增代理人培训记录操作：{}",brokerTrainingRecordForm.toString());
        ImcBrokerTrainingRecord record = new ImcBrokerTrainingRecord();
        BeanUtils.copyProperties(brokerTrainingRecordForm,record);
        record.setCreateTime(DateUtils.getNowDate());
        record.setCreateId(SecurityUtils.getUsername());
        record.setIsDeleted(IsDeletedEnum.N.getCode());
        record.setTenantId(SecurityUtils.getTenantId());
        return imcBrokerTrainingRecordMapper.insert(record);
    }

    /**
     * 修改代理人培训记录
     *
     * @param brokerTrainingRecordForm 代理人培训记录
     * @return 结果
     */
    @Override
    public int updateImcBrokerTrainingRecord(BrokerTrainingRecordForm brokerTrainingRecordForm)
    {
        logger.info(" 修改代理人培训记录操作：{}",brokerTrainingRecordForm.toString());
        //老数据删除
        ImcBrokerTrainingRecord record = this.imcBrokerTrainingRecordMapper.selectById(brokerTrainingRecordForm.getId());
        record.setIsDeleted(record.getId().toString());
        this.imcBrokerTrainingRecordMapper.updateById(record);

        //最新记录新增
        ImcBrokerTrainingRecord newRecord = new ImcBrokerTrainingRecord();
        BeanUtils.copyProperties(brokerTrainingRecordForm,newRecord);
        newRecord.setId(null);
        newRecord.setUpdateTime(DateUtils.getNowDate());
        newRecord.setUpdateId(SecurityUtils.getUsername());
        newRecord.setCreateTime(DateUtils.getNowDate());
        newRecord.setCreateId(SecurityUtils.getUsername());
        newRecord.setTenantId(SecurityUtils.getTenantId());
        return imcBrokerTrainingRecordMapper.insert(newRecord);
    }

    /**
     * 批量删除代理人培训记录
     *
     * @param ids 需要删除的代理人培训记录ID
     * @return 结果
     */
    @Override
    public int deleteImcBrokerTrainingRecordByIds(Long[] ids)
    {
        logger.info("批量删除代理人培训记录操作：{}",ids);
        int count = 0;
        for(Long id:ids){
            ImcBrokerTrainingRecord record = this.imcBrokerTrainingRecordMapper.selectById(id);
            record.setIsDeleted(record.getId().toString());
            record.setUpdateId(SecurityUtils.getUsername());
            record.setUpdateTime(DateUtils.getNowDate());
            count += this.imcBrokerTrainingRecordMapper.updateById(record);
        }
        return count;
    }
}
